package com.wy.fontchangecolor;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Time:7/30/21
 * Author:ypf
 * Description:
 */
public class FontChangeColor extends androidx.appcompat.widget.AppCompatTextView {

    private String TAG = "FontChangeColor";

    private TextPaint mOldPaint;

    private int mOrientation;
    private int mProgress;

    public FontChangeColor(@NonNull Context context) {
        this(context, null, 0);
    }

    public FontChangeColor(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FontChangeColor(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMeasureSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMeasureSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        Log.i(TAG, "widthMeasureSpecMode:" + widthMeasureSpecMode + ",heightMeasureSpecMode:" + heightMeasureSpecMode);

        int widthMeasureSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMeasureSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        Log.i(TAG, "widthMeasureSpecSize:" + widthMeasureSpecSize + ",heightMeasureSpecSize:" + heightMeasureSpecSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //不用系统的，我们自己来画
        super.onDraw(canvas);

        mOldPaint = getPaint();
        mOldPaint.setColor(getResources().getColor(R.color.color_ff0000));

        String text = getText().toString();
        int measureWidth = getMeasuredWidth();
        int measureHeight = getMeasuredHeight();
        Log.i(TAG, "measureWidth:" + measureWidth + ",measureHeight:" + measureHeight);

        Rect textBounds = new Rect();
        mOldPaint.getTextBounds(text, 0, text.length(), textBounds);
        int textBoundsWidth = textBounds.width();

        int paddingLeft = (measureWidth - textBoundsWidth) / 2;

        //计算基线的坐标
        Paint.FontMetricsInt fontMetrics = mOldPaint.getFontMetricsInt();
        int leading = fontMetrics.leading;
        int ascent = fontMetrics.ascent;
        int descent = fontMetrics.descent;
        int top = fontMetrics.top;
        int bottom = fontMetrics.bottom;
        Log.i(TAG, "leading:" + leading + ",ascent:" + ascent + ",descent:" + descent + ",top:" + top + ",bottom:" + bottom);
        Log.i(TAG, "width:" + getWidth() + ",height:" + getHeight());
        int dy = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        int baseLine = getHeight() / 2 + dy;
        Log.i(TAG, "dy:" + dy + ", baseLine:" + baseLine);

        int cleft;
        if (1 == mOrientation) {
            cleft = 0 + paddingLeft;
        } else {
            cleft = measureWidth - (int) (measureWidth * (mProgress / 100f));
        }
        int ctop = 0;
        int cright;
        if (1 == mOrientation) {
            cright = (int) (measureWidth * (mProgress / 100f));
        } else {
            cright = measureWidth - paddingLeft;
        }
        int cbootm = measureHeight;
        Log.i(TAG, "cleft:" + cleft + ",cright:" + cright + ",ctop:" + ctop + ",cbootm:" + cbootm);
        canvas.save();
        canvas.clipRect(cleft, ctop, cright, cbootm);
        canvas.drawText(text, 0, text.length(), paddingLeft, baseLine, mOldPaint);
        canvas.restore();

    }

    /**
     * 设置改变颜色
     *
     * @param orientation 1从左到右 2从右到左
     */
    public void setChangeTextColor(int orientation, int progress) {
        this.mOrientation = orientation;
        this.mProgress = progress;
        invalidate();
    }

}
